package org.example.wu_and_dian.controller;

import org.example.wu_and_dian.annotation.TokenRequired;
import org.example.wu_and_dian.dto.request.CreateProjectRequest;
import org.example.wu_and_dian.dto.request.ProjectRequest;
import org.example.wu_and_dian.dto.request.UpdateProjectRequest;
import org.example.wu_and_dian.dto.response.AjaxResult;
import org.example.wu_and_dian.entity.Project;
import org.example.wu_and_dian.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.example.wu_and_dian.dto.response.ProjectResponse;

@RestController
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @TokenRequired
    @PostMapping("/get_project_list")
    public AjaxResult getProjectList(@RequestBody ProjectRequest projectRequest) {
        String email = projectRequest.getEmail();
        List<Project> projects = projectService.getProjectsByEmail(email);
        List<ProjectResponse> responses = projects.stream()
                .map(project -> new ProjectResponse(project.getId(), project.getName(), project.getDescription(), project.getCreatedAt(), project.getUpdatedAt()))
                .collect(Collectors.toList());
        System.out.println(responses);
        return AjaxResult.success(responses);
    }
    @TokenRequired
    @PostMapping("/create_project")
    public AjaxResult createProject(@RequestBody CreateProjectRequest createProjectRequest) {
        String email = createProjectRequest.getEmail();
        String name = createProjectRequest.getName();
        String description = createProjectRequest.getDescription();
        System.out.println("email:"+email+"name:"+name+"description:"+description);
        int insert=projectService.createProject(email, name, description);
        if (insert==0){
            return AjaxResult.error("新项目创建失败");
        }
        return AjaxResult.success("新项目创建成功");
    }
    @TokenRequired
    @PostMapping("/get_project")
    public AjaxResult getProject(@RequestBody Map<String, String> requestBody) {
        String projectId = requestBody.get("projectId");
        Project project = projectService.getProjectById(projectId);
        ProjectResponse response = new ProjectResponse(
                project.getId(),
                project.getName(),
                project.getDescription(),
                project.getCreatedAt(),
                project.getUpdatedAt()
        );
        return AjaxResult.success(response);
    }
    @TokenRequired
    @DeleteMapping("/project/delete/{projectId}")
    public AjaxResult deleteProject(@PathVariable String projectId) {
        int affectedRows = projectService.deleteProjectById(projectId);
        return new AjaxResult()
                .put("success", affectedRows > 0)
                .put("message", affectedRows > 0 ? "删除成功" : "文档不存在")
                .put("num", affectedRows);
    }
    @TokenRequired
    @PutMapping("/update_project/{projectId}")
    public AjaxResult updateProject(@PathVariable String projectId, @RequestBody UpdateProjectRequest request) {
        int updatedRows = projectService.updateProject(projectId, request);
        if (updatedRows > 0){
            return AjaxResult.success("更新成功", updatedRows);
        }else {
            return AjaxResult.error("更新失败，未知错误");
        }
    }
}

